from django.utils.deprecation import MiddlewareMixin
from api.utils import resultResponse
from api.commonlog import CommonLog
from h5api.settings import WHITE_LIST,MALLAPI_WHITE_LIST

class MyMiddleware(MiddlewareMixin):

    def process_request(self,request):
        signal = request.path + request.method 
        #confirm login 
        if 'mallapi' in signal:
            if signal in MALLAPI_WHITE_LIST:
                pass 
            else:
                consumer_id = request.session.get("consumer_id",None)
                if not consumer_id:
                    return resultResponse(201) 
        else:
            if signal in WHITE_LIST:
                pass
            else:
                serviceProvider_id = request.session.get("serviceProvider_id",None) 
                if not serviceProvider_id:
                    return resultResponse(201) 

    def process_response(self,request,response):
        return response

    def process_view(self,request,view_func,view_args,view_kwargs):
        pass 

    def process_exception(self,request,exception):
        #logging
        log = CommonLog()
        message = exception
        log.console(message)
        return resultResponse(502)